home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / src / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  2.9 KB  |  181 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #include <assert.h>
  32. #include "list.h"
  33. #include "sys.stuff.h"
  34.  
  35. /*
  36.  * Keep a list of free list nodes for fast allocation.
  37.  */
  38.  
  39. static LIST free_list_list = NULL;
  40.  
  41. /*
  42.  * Make a list object and initialize its values to something decent.
  43.  */
  44.  
  45. LIST
  46. make_list ()
  47. {
  48.     LIST list;
  49.  
  50.     if (free_list_list)
  51.     {
  52.         list = free_list_list;
  53.         free_list_list = list_next (free_list_list);
  54.     }
  55.     else
  56.     {
  57.         list = (LIST) ALLOC (sizeof (_LIST));
  58.     }
  59.  
  60.     set_list_datum (list, NULL);
  61.     set_list_next (list, NULL);
  62.  
  63.     return list;
  64. }
  65.  
  66. /*
  67.  * Free a list by placing it on the free list.
  68.  */
  69.  
  70. void
  71. free_list (list)
  72.     LIST list;
  73. {
  74.     set_list_next (list, free_list_list);
  75.     free_list_list = list;
  76. }
  77.  
  78. /*
  79.  * Return the nth node of a list. (recursive)
  80.  */
  81.  
  82. LIST
  83. list_nth (list, n)
  84.     LIST list;
  85.     short n;
  86. {
  87.     if (list != NULL)
  88.         if (n > 0)
  89.             return list_nth (list_next (list), n - 1);
  90.  
  91.     return list;
  92. }
  93.  
  94. /*
  95.  * Append a list.
  96.  */
  97.  
  98. LIST
  99. list_append_list (set, lst)
  100.     LIST set, lst;
  101. {
  102.     LIST temp;
  103.  
  104.     if (!set)
  105.     {
  106.         return lst;
  107.     }
  108.     else
  109.     {
  110.         temp = set;
  111.         while (list_next (temp))
  112.             temp = list_next (temp);
  113.  
  114.         set_list_next (temp, lst);
  115.         set_list_prev (lst, temp);
  116.  
  117.         return set;
  118.     }
  119. }
  120.  
  121. /*
  122.  * Prepend a list.
  123.  */
  124.  
  125. LIST
  126. list_prepend_list (set, lst)
  127.     LIST set, lst;
  128. {
  129.     if (set)
  130.         set_list_prev (set, lst);
  131.     set_list_next (lst, set);
  132.     set_list_prev (lst, NULL);
  133.  
  134.     return lst;
  135. }
  136.  
  137. /*
  138.  * Remove the node that contains "datum"
  139.  */
  140.  
  141. LIST
  142. list_remove_list (set, datum)
  143.     LIST set;
  144.     void *datum;
  145. {
  146.     LIST tmp;
  147.  
  148.     if (!set)
  149.         return NULL;
  150.  
  151.     if (list_datum (set) == datum)
  152.     {
  153.         tmp = set;
  154.         set = list_next (set);
  155.         if (set && list_prev (set))
  156.             set_list_prev (set, NULL);
  157.         free_list (tmp);
  158.     }
  159.     else
  160.     {
  161.         tmp = list_next (set);
  162.         while (tmp)
  163.         {
  164.             if (list_datum (tmp) == datum)
  165.             {
  166.                 if (list_prev (tmp))
  167.                     set_list_next (list_prev (tmp), list_next (tmp));
  168.                 if (list_next (tmp))
  169.                     set_list_prev (list_next (tmp), list_prev (tmp));
  170.  
  171.                 free_list (tmp);
  172.                 break;
  173.             }
  174.  
  175.             tmp = list_next (tmp);
  176.         }
  177.     }
  178.  
  179.     return set;
  180. }
  181.